package com.labManage.controller;

import com.labManage.dao.KindsDao;
import com.labManage.entity.Kinds;
import com.labManage.service.KindsService;
import com.labManage.vo.KindVo;
import com.labManage.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/kinds")
public class KindsController {

    //Controller层需要一个service对象，用于调用对应的业务处理
    @Autowired
    private KindsService kindsService;
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private KindsDao kindsDao;
    /***************************************************************************************************************************
     *  @fn             getAllKinds
     *  @brief          controller方法
     *  @type           非静态方法
     *  @detail         当前端请求访问/getAllKinds时找到与之对应的controller去处理这个请求
     *  @param[in]      无
     *  @return         ResultVo        返回获取所有种类的响应信息，包括响应状态、响应消息、响应数据
     *  @author         杜健华，杜志华
     *  @date           2021.06.21
     ****************************************************************************************************************************/
    @RequestMapping("/getAllKinds")
    @ResponseBody
    public ResultVo getAllKinds() {
        try{
            //查询数据库中的kinds表获取所有种类的名称，描述，库存数量，总数量等信息
            List<KindVo> kindVoList = kindsService.queryAllKind();
            //返回查询结果
            return ResultVo.build(kindVoList.size(),0,"",kindVoList);
        }catch (Exception e){
            return ResultVo.build(500,"服务器异常");
        }
    }

    /***************************************************************************************************************************
     *  @fn             addKinds
     *  @brief          controller方法
     *  @type           非静态方法
     *  @detail         当前端请求访问/addKinds时找到与之对应的controller去处理这个请求
     *  @param[in]      Kinds  kinds    需要添加的种类的一系列信息，包括分类名称，描述，图片信息
     *  @return         ResultVo        返回获取所有种类的响应信息，包括响应状态、响应消息、响应数据
     *  @author         杜健华，杜志华
     *  @date           2021.06.21
     ****************************************************************************************************************************/
    @RequestMapping("/addKindsInfo")
    @ResponseBody
    @RequiresPermissions("kinds:add")
    public ResultVo addKinds(Kinds kinds){
        //如果前端传过来的数据不为空，则判断分类名称是否为空
        if(null!=kinds){
            //如果不为空，则调用对应的service方法，将数据插入到数据库中
            if(false=="".equals(kinds.getKindsName())){
                try{
                    //调用service方法addKinds(kinds)
                    int result = kindsService.addKinds(kinds);
                    //如果插入结果返回0，则表示该类名称已经在数据库中存在
                    if(0==result){
                        return ResultVo.build(201,"该类名已经存在");
                    }else{
                        return ResultVo.build(0,"增加成功");
                    }
                }catch (Exception e){
                    return ResultVo.build(500,"服务器异常");
                }
            }else {
                return ResultVo.build(200,"请输入类别名称");
            }
        }else{
            return ResultVo.build(200,"请输入相关信息");
        }
    }

    @RequestMapping("/getKindsInfoByKindsName")
    @ResponseBody
    public ResultVo getKindsInfoByKindsName(String kindsType){
        if(null==kindsType){
            HttpSession session = request.getSession();
            String Type = (String) session.getAttribute("kindsType");
            if(null==Type){
                try {
                    List<KindVo> kindVoList = kindsService.queryAllKind();
                    return ResultVo.build(200,"",kindVoList.get(0));
                }catch (Exception e){

                }
            }else{
                try {
                    Kinds kinds = kindsService.getKindsByKindsName(Type);
                    return ResultVo.build(200,"",kinds);
                }catch (Exception e){

                }
            }
        }else{
            try {
                Kinds kinds = kindsService.getKindsByKindsName(kindsType);
                return ResultVo.build(200,"",kinds);
            }catch (Exception e){

            }
        }
        return ResultVo.build(201,"");
    }

    @RequestMapping("/getKindsNum")
    @ResponseBody
    public ResultVo getKindsNum(){
        long kindsNum = kindsDao.selectKindsNum(new Kinds());
        return ResultVo.build(0,"",kindsNum);
    }

    @RequestMapping("/getAllKindsLimit")
    @ResponseBody
    public ResultVo getAllRoleLimit(Integer page,Integer limit,Kinds condition){
        try{
            List<Kinds> roleList = kindsService.getAllKindsLimit(page,limit,condition);
            return ResultVo.build(roleList.size(),0,"",roleList);
        }catch (Exception e){
            return ResultVo.build(500,"服务器异常");
        }
    }

    @RequestMapping("/modifyKindsInfo")
    @ResponseBody
    @RequiresPermissions("kinds:modify")
    public ResultVo modifyKindsInfo(Kinds kinds){
        int result = kindsService.modifyKindsInfo(kinds);
        if(result>0){
            return ResultVo.build(0,"修改成功");
        }else{
            if(result==-1){
                return ResultVo.build(201,"种类名已存在");
            }
            return ResultVo.build(201,"修改失败");
        }
    }

    @RequestMapping("/deletekindsInfo")
    @ResponseBody
    @RequiresPermissions("kinds:delete")
    public ResultVo deletekindsInfo(Long id){
        int result = kindsService.deleteKindsInfoById(id);
        if(result>0){
            return ResultVo.build(0,"删除成功");
        }else{
            return ResultVo.build(201,"该种类已存在绑定的物品，无法删除");
        }
    }
}
